package com.penjin.android.activity.kaoqin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.penjin.android.R;
import com.penjin.android.domain.PenjinCompany;
import com.penjin.android.domain.PenjinDate;
import com.penjin.android.domain.PenjinStaff;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.service.UserService;
import com.penjin.android.utils.BitmapUtils;
import com.penjin.android.utils.JsonUtils;
import com.penjin.android.view.CircleImageView;
import com.penjin.android.view.CustomProgressDialog;
import com.penjin.android.view.TitleBarView;
import com.penjin.android.widget.calendar.CalendarTool;
import com.penjin.android.widget.calendar.DateEntity;
import com.penjin.android.widget.calendar.PenjinCalendar;
import com.penjin.android.widget.calendar.PenjinCalendarAdapter;
import com.penjin.android.widget.calendar.PenjinCalendarPoint;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 个人排班 :工作日历界面
 * Created by maotiancai on 2016/1/19.
 */
public class GerenPaibanActivityNew extends Activity implements View.OnClickListener {

    PenjinUser user;
    PenjinCompany company;
    PenjinStaff staff;

    private List<DateEntity> mDateEntityList;
    private PenjinCalendarPoint mNowCalendarPoint;
    private PenjinCalendarPoint pickPoint;
    private CalendarTool mCalendarTool;
    CalendarTool.CalendarCallback calendarCallback;
    private PenjinCalendar penjinCalendar;
    private PenjinCalendarAdapter calendarAdapter;

    TitleBarView titleBar;
    CircleImageView avatar;
    TextView name;
    TextView department;
    TextView zhiwu;
    TextView workDays;//该月需要上的班数

    TextView year;
    TextView month_day;

    View leftDayBtn;
    View rightDayBtn;

    private CustomProgressDialog progressDialog;
    private PenjinDate todayDate;
    private PenjinDate pickDate;

    private boolean isCurrentMonthSet = false;
    private boolean isGettingDataFromServer = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_geren_paiban);
        initData();
        initView();
        initTitleBar();
        initDate();
    }

    private void initData() {
        user = UserService.getInstance(this.getApplicationContext()).getCurrentUser();
        if (user == null) {
            Toast.makeText(GerenPaibanActivityNew.this, "用户尚未登陆！", Toast.LENGTH_SHORT).show();
            finish();
        }
        Toast.makeText(this, user.getPhone() + " " + user.getStaffNum(), Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        progressDialog = CustomProgressDialog.createDialog(this);
        progressDialog.setMessage("请稍后...");
        penjinCalendar = (PenjinCalendar) findViewById(R.id.calendar);
        progressDialog = CustomProgressDialog.createDialog(this);
        avatar = (CircleImageView) findViewById(R.id.avatar);
        name = (TextView) findViewById(R.id.name);
        department = (TextView) findViewById(R.id.department);
        zhiwu = (TextView) findViewById(R.id.zhiwu);

        year = (TextView) findViewById(R.id.year);
        month_day = (TextView) findViewById(R.id.month_day);
        leftDayBtn = (TextView) findViewById(R.id.leftDayBtn);
        rightDayBtn = (TextView) findViewById(R.id.rightDayBtn);

        titleBar = (TitleBarView) findViewById(R.id.titleBar);

        leftDayBtn.setOnClickListener(this);
        rightDayBtn.setOnClickListener(this);
        BitmapUtils.loadUrlImage(user.getTouxiang(), avatar, this, false);
        if (user.isInCompany()) {
            UserService userService = UserService.getInstance(this.getApplicationContext());
            company = userService.getCurrentCompany();
            staff = userService.getCurrentStaff();
            name.setText(JsonUtils.isJsonNull(staff.getRealName())?user.getUsername():staff.getRealName());
            department.setText(JsonUtils.isJsonNull(staff.getDepartment())?"暂未分配部分":staff.getDepartment());
            zhiwu.setText(JsonUtils.isJsonNull(staff.getPosition())?"":staff.getPosition());
        }
    }

    private void initTitleBar() {
        titleBar = (TitleBarView) findViewById(R.id.titleBar);
        titleBar.setTitleBarListener(new TitleBarView.TitleBarListener() {
            @Override
            public void left(View view) {
                finish();
            }

            @Override
            public void center(View view) {

            }

            @Override
            public void right(View view) {

            }
        });
    }

    private void initDate() {
        calendarCallback = new CalendarTool.CalendarCallback() {
            @Override
            public void onFetchDataFromServerSucess(List<DateEntity> dateEntityList) {
                isGettingDataFromServer=false;
                progressDialog.dismiss();
                for (DateEntity entity : dateEntityList) {
                    System.out.println(entity.year + " " + entity.month + " " + entity.day + " " + entity.text);
                }
                calendarAdapter = new PenjinCalendarAdapter(GerenPaibanActivityNew.this, GerenPaibanActivityNew.this.getResources());
                calendarAdapter.setDateList(dateEntityList);
                isCurrentMonthSet = true;
                penjinCalendar.setAdapter(calendarAdapter);
            }

            @Override
            public void onFetchDataFromServerError(String msg) {
                isGettingDataFromServer=false;
                progressDialog.dismiss();
            }
        };
        Calendar calendar = Calendar.getInstance();
        todayDate = new PenjinDate(calendar.getTime());//当前日期
        pickDate = todayDate;//当前选择的日期
        setDateRegion(pickDate);
        mCalendarTool = new CalendarTool(this);
        mCalendarTool.setCalendarCallback(calendarCallback);
        mNowCalendarPoint = mCalendarTool.getNowCalendar();
        pickPoint=new PenjinCalendarPoint();
        pickPoint.x=mNowCalendarPoint.x;
        pickPoint.y=mNowCalendarPoint.y;
        penjinCalendar.setAdapter(calendarAdapter);
    }

    //设置显示日期的区域
    private void setDateRegion(PenjinDate penjinDate) {
        year.setText(penjinDate.year + "年");
        month_day.setText(penjinDate.month + "月" + penjinDate.date + "日" + "(" + penjinDate.weekDay + ")");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftDayBtn:
                if(!isGettingDataFromServer){
                    pickPoint=mCalendarTool.getPreCalendarPoint(pickPoint);
                    getStaffWorkCanlendar();
                }
                break;
            case R.id.rightDayBtn:
                if(!isGettingDataFromServer)
                {
                    pickPoint=mCalendarTool.getNextCalendarPonint(pickPoint);
                    getStaffWorkCanlendar();
                }
                break;
            default:
                break;
        }
    }

    private void getStaffWorkCanlendar() {
        if (user.isInCompany()) {
            isGettingDataFromServer=true;
            progressDialog.show();
            mCalendarTool.getDateEntityListFromServer(pickPoint.x, pickPoint.y, staff.getStaffNumber());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isCurrentMonthSet) {
            getStaffWorkCanlendar();
        }
    }
}
